package com.mangowang.module.main.ui;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.google.protobuf.InvalidProtocolBufferException;
import com.mangowang.lib.common.base.BaseActivity;
import com.mangowang.lib.common.net.Apis;
import com.mangowang.lib.common.net.NetworkManager;
import com.mangowang.lib.common.net.interfaces.OnRequestCallBack;
import com.mangowang.lib.common.proto.Mystock;
import com.mangowang.lib.common.util.StatusBarUtil;
import com.mangowang.lib.common.util.ValidateUtils;
import com.mangowang.lib.common.view.pinnedheader.PinnedHeaderItemDecoration;
import com.mangowang.lib.common.view.pinnedheader.PinnedHeaderRecyclerView;
import com.mangowang.lib.common.view.recyclerexpand.expand.ExpandGroupItemEntity;
import com.mangowang.module.main.R;
import com.mangowang.module.main.R2;
import com.mangowang.module.main.adapter.PatrolGroupAdapter;
import com.mangowang.module.main.bean.TitlePatrolItem;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MachinesNextActivity extends BaseActivity {

    @BindView(R2.id.back_iv)
    ImageView backIv;
    @BindView(R2.id.tv_title)
    TextView tvTitle;
    @BindView(R2.id.tv_title_right)
    TextView tvTitleRight;
    @BindView(R2.id.recycler_order_list)
    PinnedHeaderRecyclerView mRecyclerView;
    @BindView(R2.id.empty_view)
    LinearLayout emptyView;
    private int position = 0;


    private LinearLayoutManager mLayoutManager;
    private PatrolGroupAdapter mAdapter;
    private List<ExpandGroupItemEntity<TitlePatrolItem, Mystock.myMachineToolsList.List>> expandGroupItemEntities;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_machines_next);
        ButterKnife.bind(this);
        StatusBarUtil.setStatusBarLightMode(this, Color.parseColor("#FFFFFF"), true);
        initView();
        initEvent();
        initData();
    }

    private void initView() {
        tvTitle.setText(getIntent().getStringExtra("title"));
        mRecyclerView.setLayoutManager(mLayoutManager = new LinearLayoutManager(mContext));
        mRecyclerView.addItemDecoration(new PinnedHeaderItemDecoration());
    }

    private void initEvent() {
        /**
         * 当标题栏被悬浮的时候的点击功能
         */
        mRecyclerView.setOnPinnedHeaderClickListener(adapterPosition -> {
            mAdapter.switchExpand(adapterPosition);
            //标题栏被点击之后，滑动到指定位置
            mLayoutManager.scrollToPositionWithOffset(adapterPosition, 0);
        });
        backIv.setOnClickListener(v -> finish());
    }

    private void initData() {
        Mystock.myMachineToolsList.Builder builder = Mystock.myMachineToolsList.newBuilder();
        builder.setMachineType(getIntent().getAction()).setGoodsId(getIntent().getType());
        NetworkManager.INSTANCE.post(Apis.myMachineToolsList, builder.build().toByteArray(), new OnRequestCallBack() {
            @Override
            public void onOk(byte[] bytes) throws InvalidProtocolBufferException {
                Mystock.myMachineToolsList result = Mystock.myMachineToolsList.parseFrom(bytes);
                mAdapter = new PatrolGroupAdapter();
                expandGroupItemEntities = obtainDataList(result.getArrayListList());
                expandGroupItemEntities.addAll(obtainDataList(result.getOtherListList()));
                mAdapter.setData(expandGroupItemEntities);
                mRecyclerView.setAdapter(mAdapter);

                if (ValidateUtils.isValidate(expandGroupItemEntities)) {
                    mRecyclerView.setVisibility(View.VISIBLE);
                    emptyView.setVisibility(View.GONE);
                } else {
                    mRecyclerView.setVisibility(View.GONE);
                    emptyView.setVisibility(View.VISIBLE);
                }

            }

            @Override
            public void onError(int code, String errorMessage) {
                hideProgress();
                mRecyclerView.setVisibility(View.GONE);
                emptyView.setVisibility(View.VISIBLE);
            }
        });


    }

    private List<ExpandGroupItemEntity<TitlePatrolItem, Mystock.myMachineToolsList.List>>
    obtainDataList(List<Mystock.myMachineToolsList.Lists> datas) {
        List<ExpandGroupItemEntity<TitlePatrolItem, Mystock.myMachineToolsList.List>> dataList = new ArrayList<>();
        for (int group = 0; group < datas.size(); group++) {
            ExpandGroupItemEntity<TitlePatrolItem, Mystock.myMachineToolsList.List> groupItem = new ExpandGroupItemEntity<>();
            groupItem.setExpand(false);
            TitlePatrolItem titleBean = new TitlePatrolItem();
            titleBean.setMachines_number(datas.get(group).getNum());
            titleBean.setSn_number(datas.get(group).getTittle());
            titleBean.setPosition(position);
            groupItem.setParent(titleBean);
            position++;
            List<Mystock.myMachineToolsList.List> childList = new ArrayList<>();
            for (int child = 0; child < datas.get(group).getSnListCount(); child++) {
                childList.add(datas.get(group).getSnList(child));
            }
            groupItem.setChildList(childList);
            dataList.add(groupItem);
        }
        return dataList;
    }
}
